import api from '~api/index'
import util from '~src/lib/util'
export default {
  state: {
    user_info: {}
  },
  mutations: {
    ['setUserInfo'] (state, info) {
      state.user_info = info
      util.localItem.set('telephone', info.telephone)
    },
    ['editUserInfo'] (state, info) {
      state.user_info = info
      util.localItem.set('telephone', info.telephone)
    }
  },
  actions: {
    async ['getUserInfo'] ({commit}, from) {
      const { data: { data, code } } = await api.post('/v1/getUserInfo', from)
      if (data && code === '200') {
        commit('setUserInfo', data)
      }
    },
    //更改用户信息
    async ['changeUserInfo'] ({commit}, config) {
      const { data: { data, code,msg } } = await api.post('/v1/changeUserInfo', config)
      if (data && code === '200') {
        commit('editUserInfo', data)
        commit('TOAST_SHOW', {content: '编辑成功', msg: true})
        location.href = '/vip'
      } else {
        commit('TOAST_SHOW', {content: msg, msg: true})
      }
    },
     // 编辑手机号码
    async ['eidtTelephone'] ({commit}, from) {
      const { data: { data, code } } = await api.post('/v1/changePhone', from)
      if (data && code === '200') {
        commit('setUserInfo', data)
      }else {
        commit('TOAST_SHOW', {content: msg, msg: true})
      }
    },
    async ['getSmsCode'] ({commit}, from) {
      const { data: { msg} } = await api.post('/v1/getSmsCode', {openid: util.localItem.get('openid'),
        telephone: from.telephone,codeType: '0'})
      if (data && code === '200') {
        commit('TOAST_SHOW', {content: '发送成功', msg: true})
      } else {
        commit('TOAST_SHOW', {content: msg, msg: true})
      }
    },
    async ['telephoneBind'] ({commit}, from) {
      const { data: { data,code, msg} } = await api.post('/v1/bind', {openid: util.localItem.get('openid'),
        telephone: from.telephone,verifyCode: from.verifyCode})
      if (data && code === '200') {
        commit('TOAST_SHOW', {content: '绑定成功', msg: true})
      } else {
        commit('TOAST_SHOW', {content: msg, msg: true})
      }
    }
  },
  getters: {
    ['getUserInfo'] (state) {
      return state.user_info
    }
  }
}
